import axios from 'axios';
import {getCookie} from "../utils/util";

// 登录请求方法
const loginrequet = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        return ret
      }
    ]
  }).then(res => res.data);
};
// 通用公用方法
const request = (method, url, params) => {
  return axios({
    method: method,
    url: url,
    headers: {
      'Content-Type': 'application/json',
      token: localStorage.getItem('token'),
      //cookie中的token ，根据这个来判断是否需要鉴权，一般都需要使用
      'user-token': getCookie("user-token")
    },
    data: params,
    traditional: true,
    transformRequest: [
      function (data) {
        let ret = ''
        for (let it in data) {
          ret +=
            encodeURIComponent(it) +
            '=' +
            encodeURIComponent(data[it]) +
            '&'
        }
        return ret
      }
    ]
  }).then(res => res.data);
};

let url_tmp = (url) => {
  let url_tmp = url.indexOf('public') > -1 ? url : `/public${url}`
  return url_tmp;
}


export {
  request,
  url_tmp,
  loginrequet
}
